  var baseurl = "http://10.222.29.195:8999/admin/";

    $(function () {
        //渲染未审核的记录
        $("#uncheckRecord").click(function (e) {
            $(".treeview").addClass("menu-open");
            $(".active").removeClass("active");
            $(e.target).parent().addClass("active");
            $(".col-xs-12").css('display', 'none');
            ajaxStauts(0,1);
        });
        //渲染审核通过的记录
        $("#passRecord").click(function (e) {
            $(".treeview").addClass("menu-open");
            $(".active").removeClass("active");
            $(e.target).parent().addClass("active");
            $(".col-xs-12").css('display', 'none');
            ajaxStauts(1,1);
        });
        //渲染审核不通过的记录
        $("#noPassRecord").click(function (e) {
            $(".treeview").addClass("menu-open");
            $(".active").removeClass("active");
            $(e.target).parent().addClass("active");
            $(".col-xs-12").css('display', 'none');
            ajaxStauts(2,1);
        });
        //渲染驳回的记录
        $("#rejectRecord").click(function (e) {
            $(".treeview").addClass("menu-open");
            $(".active").removeClass("active");
            $(e.target).parent().addClass("active");
            $(".col-xs-12").css('display', 'none');
            ajaxStauts(3,1);
        });



        //页面加载渲染未审核的记录
        ajaxStauts(0,1);

        //修改审核状态的ajax
        function ajaxUpdateStauts(id, status,pageNum) {
            $.ajax({
                url: baseurl + "order/update",
                type: "POST",
                data: {"id": id, "status": status,"page" : pageNum},
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    render(status,data.content,pageNum);

                }
            });
        }

        //加载指定状态的审核记录
        function ajaxStauts(status,pageNum) {
            $.ajax({
                url: baseurl + "ordersByStatus?status=" + status + "&page=" + pageNum,
                type: "GET",
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    render(status,data.content,pageNum);
                }
            });
        }


        /**
         * 渲染指定的状态和页码的数据
         * @param obj
         */
        function render(status,obj,pageNum) {
            if(obj == false){
                window.location = "http://10.222.29.195:9000/login.html"
            }

            //移除除了第一个隐藏的其他卡片控件
            $(".col-md-4:gt(0)").remove();
            //移除全部的页码
            $("#page li").remove();
            if(obj.pageTotal > 1 && obj.items.length == 0){
                //当总页数大于1但是当前的卡片数量为0，说明未审核的记录被操作了，返回前一页的未审核记录
                ajaxStauts(0,pageNum - 1);
            }else{

                if(obj.pageTotal > 1){
                    for(let i = 1; i <= obj.pageTotal; i++){
                        let curPage;
                        if(obj.pageIndex == i){
                            curPage = $("<li class='active'><a href='#'>"+ i +"</a></li>");
                        }else{
                            curPage = $("<li><a href='#'>"+ i +"</a></li>");
                        }
                        curPage.click(function(e){
                            ajaxStauts(status,i);
                            e.preventDefault();
                        });
                        $("#page").append(curPage);
                    }
                }
                var createElementEg = document.createDocumentFragment();
                obj.items.forEach(item => {
                    let clone = $(".col-md-4:first").clone();
                    clone.css('display', 'block');
                    clone.find("#storeName").text(item.store.name);
                    clone.find("#marchentName").text(item.identity.name);
                    clone.find("#idCard").text(item.identity.idCardNumber);
                    clone.find("#license").text(item.store.license);
                    clone.find("#address").text(item.store.address);
                    clone.find("#idCardPhoto").attr("src", "http://10.222.29.157:8080/res/" + item.identity.idCardPic);
                    if (item.storeFormRecord.status == 0) {
                        clone.find(".box,.box-default").attr("class", "box box-warning");
                        //点击通过按钮
                        clone.find("#successOperate").click(function (e) {
                            ajaxUpdateStauts(item.storeFormRecord.id, 1,pageNum);
                            e.preventDefault();
                        });
                        //点击不通过
                        clone.find("#failOperate").click(function (e) {
                            ajaxUpdateStauts(item.storeFormRecord.id, 2,pageNum);
                            e.preventDefault();
                        });
                        //点击驳回
                        clone.find("#replyOperate").click(function (e) {
                            ajaxUpdateStauts(item.storeFormRecord.id, 3,pageNum);
                            e.preventDefault();
                        });

                    } else if (item.storeFormRecord.status == 1) {
                        clone.find(".box,.box-default").attr("class", "box box-success");
                        clone.find("#statusOperate").html("<h3 style='color: green'><b>通过</b></h3>");
                    } else if (item.storeFormRecord.status == 2) {
                        clone.find("#statusOperate").html("<h3 style='color: darkred'><b>不通过</b></h3>");
                        clone.find(".box,.box-default").attr("class", "box box-danger");
                    } else {
                        clone.find("#statusOperate").html("<h3 style='color: grey'><b>已驳回</b></h3>");
                    }
                    createElementEg.appendChild(clone[0]);
                });
                $(".row").append(createElementEg);

            }

        }

    });
